<?php

require('configs/include.php');

class c_Crear_Nuevo_Diseno extends super_controller {

    public function preparar_pcontrol() {
        $options['fase']['lvl2'] = "all";

        $this->orm->connect();
        $this->orm->read_data(array("fase"), $options);
        $fase = $this->orm->get_objects("fase");
        $this->orm->close();
        //print_r2($fase);
        $this->engine->assign('fase', $fase);
    }
    public function cancelar() {
        session_destroy();
        unset($this->session);

        header('Location: http://localhost/glight/index.php');
    }
    public function limpiar(){
                $_SESSION['DISEÑADOR']['parametros']=null;

    }

    public function agregar() {


        $parametro = new object_standard();
        $parametro->codigo = $this->post->Codigo_PC_tpl;
        $parametro->nombre = $this->post->Nombre_PC_tpl;
        $parametro->descripcion = $this->post->Descripcion_Parametro_tpl;
        $parametro->estado = $this->post->Fase_Desarrollo_tpl;

        $parametro_control = new parametro_control($parametro);
        $_SESSION['DISEÑADOR']['parametros'][] = serialize($parametro_control);
        //var_dump($_SESSION['DISEÑADOR']['parametros']);
    }

    public function guardar_diseno() {
        
        if(is_empty($_SESSION['DISEÑADOR']['parametros'])){
            throw_exception('Parametros de control vacios');
        }

        $id = $_SESSION['empleado']['identificacion'];
        //Panel Informaci�n del dise�o
        $this->engine->assign('codigo', $this->post->Codigo_Diseno_tpl);
        $this->engine->assign('nombre', $this->post->Nombre_Diseno_tpl);
        $this->engine->assign('descripcion', $this->post->Descripcion_Diseno_tpl);

        $dataModelo = new object_standard();
        $dataModelo->codigo = $this->post->Codigo_Diseno_tpl;
        $dataModelo->nombre = $this->post->Nombre_Diseno_tpl;
        $dataModelo->descripcion = $this->post->Descripcion_Diseno_tpl;
        $dataModelo->responsable = $id;

        $modelo = new modelo($dataModelo);
        //print_r2($modelo);
        $this->orm->connect();
        $this->orm->insert_data("normal", $modelo);
        $this->orm->close();
        $this->temp_aux = 'message.tpl';

        //Panel Par�metros de control

        $this->engine->assign('Fase_Desarrollo', $this->post->Fase_Desarrollo_tpl);

        $parametros = $_SESSION['DISEÑADOR']['parametros'];

        //var_dump($parametros);
        foreach ($parametros as $p) {
            $param = unserialize($p);
            $parametro = new object_standard();
            $parametro->codigo = $param->get('codigo');
            $parametro->nombre = $param->get('nombre');
            $parametro->descripcion = $param->get('descripcion');
            $parametro->estado = $param->get('estado');
            $parametro->codigo_modelo = $modelo->get('codigo');
            
            $pcontrol=new parametro_control($parametro);
            
            $this->orm->connect();
            $this->orm->insert_data("normal", $pcontrol);
            $this->orm->close();
            //var_dump($parametro);
        }
        //  print_r2($parametro_control);
        $_SESSION['DISEÑADOR']['parametros']=null;

        $this->engine->assign('type_warning', 'success');
        $this->engine->assign('msg_warning', "Diseño creado correctamente");
        $this->temp_aux = 'message.tpl';
    }

    public function display() {

        $this->engine->assign('parametros', $_SESSION['DISEÑADOR']['parametros']);
        $this->engine->assign('title', "Crear Nuevo Diseño");
        $this->engine->display('header.tpl');
        $this->preparar_pcontrol();
        if ($this->error == 1) {
            $this->engine->assign('object', $this->post);
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
            $this->temp_aux = 'message.tpl';
        }
        $this->engine->display($this->temp_aux);
        $this->engine->display('Crear_Nuevo_Diseno.tpl');
        $this->engine->display('footer.tpl');
    }

    public function run() {

        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }/* else{
              $this->display();
              } */
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
        }
        $this->display();
    }

}

$call = new c_Crear_Nuevo_Diseno();
$call->run();
?>
